package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.dao.ToolsListDao;
import com.sugon.dao.ToolsPartInDao;
import com.sugon.dao.ToolsStockDao;
import com.sugon.entity.ToolsListEntity;
import com.sugon.entity.ToolsPartInEntity;
import com.sugon.entity.ToolsStockEntity;
import com.sugon.entity.ToolsStorePosEntity;
import com.sugon.service.ToolsListService;
import com.sugon.service.ToolsPartInService;
import com.sugon.service.ToolsStorePosService;
import com.sugon.utils.Constant;
import com.sugon.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.sugon.utils.ShiroUtils.getUserId;

/**
 * 工装--入库记录Service实现类
 *
 * @author sugon
 * @date 2019-08-02 17:51:58
 */
@Service("toolsPartInService")
public class ToolsPartInServiceImpl implements ToolsPartInService {

    @Autowired
    private ToolsPartInDao toolsPartInDao;
    @Autowired
    private ToolsStockDao toolsStockDao;
    @Autowired
    private ToolsStorePosService toolsStorePosService;
    @Autowired
    private ToolsListService toolsListService;
    @Autowired
    private ToolsListDao toolsListDao;

    @Override
    public ToolsPartInEntity queryObject(String id) {
        return toolsPartInDao.queryObject(id);
    }

    @Override
    public List<ToolsPartInEntity> queryList(Map<String, Object> map) {
        return toolsPartInDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return toolsPartInDao.queryTotal(map);
    }

    @Override
    public int save(ToolsPartInEntity toolsPartIn) {
        toolsPartIn.setId(RandomUtil.randomString(32));
        toolsPartIn.setIsDel(Constant.IS_NOT_DEL_INT);
        toolsPartIn.setCreateUser(getUserId());
        toolsPartIn.setCreateDate(new Date());
        return toolsPartInDao.save(toolsPartIn);
    }

    @Override
    public synchronized String generateUniqueCode(Object object) {
        // 声明当天的入库编码前缀
        String codePrefix = Constant.TOOL_IN + "-" + DateUtil.format(DateUtil.date(), "yyyyMMdd") + "-";
        String spareUniqueCode = (String) object;
        if (StrUtil.isEmpty(spareUniqueCode)) {
            // 查询当前最新的入库编码
            spareUniqueCode = toolsPartInDao.getLastestPartCode(codePrefix);
        }
        // 生成新的唯一编码
        spareUniqueCode = com.sugon.modules.utils.StrUtil.increase(spareUniqueCode, codePrefix, 4, 1);
        // 确保最新生成备件编码不重复
        ToolsPartInEntity toolsPartInEntity = queryByCode(spareUniqueCode);
        if (toolsPartInEntity == null) {
            return spareUniqueCode;
        }
        return generateUniqueCode(spareUniqueCode);
    }

    @Override
    public ToolsPartInEntity queryByCode(String toolsPartInCode) {
        Map<String, Object> param = new HashMap<>(1);
        param.put("inCode", toolsPartInCode);
        List<ToolsPartInEntity> toolsPartInList = toolsPartInDao.queryList(param);
        return CollectionUtil.isNotEmpty(toolsPartInList) ? toolsPartInList.get(0) : null;
    }

    @Override
    public int inbound(ToolsPartInEntity toolsPartIn) {
        toolsPartIn.setInCode(generateUniqueCode());
        toolsPartIn.setInDate(DateUtil.date());
        toolsPartIn.setOperation(getUserId());
        toolsPartIn.setOperationName(ShiroUtils.getUserEntity().getNickName());
        /*
            校验工装入库信息中查询库位是否存在
                如果存在，不做处理
                如果不存在，则根据仓库ID、工装编码和库位编码查询并设置
         */
        if (StrUtil.isEmpty(toolsPartIn.getCargoLocation())) {
            ToolsStorePosEntity spareStorePos = toolsStorePosService.queryOrCreateIfNotExists(toolsPartIn.getInStoreId(), toolsPartIn.getCargoLocationName(), toolsPartIn.getPartCode());
            toolsPartIn.setCargoLocation(spareStorePos.getId());
        }
        // 1、生成备件入库记录

        String partCode = toolsPartIn.getPartCode();
        if (StrUtil.isNotEmpty(partCode)) {
            ToolsListEntity toolsList = toolsListService.queryByCode(partCode);
            if (toolsList != null) {
                toolsPartIn.setPartUnit(toolsList.getWorkshop());
            }
        }
        int rows = save(toolsPartIn);
        if (rows > 0) {
            // 2、 实时更新仓库库位库存数
            toolsStorePosService.realtimeUpdateStorePosStock(toolsPartIn.getCargoLocation());
            // 3、实时更新备件库存数
            toolsListDao.realtimeUpdateStock(toolsPartIn.getPartCode());
        }
        return rows;
    }

    @Override
    public int update(ToolsPartInEntity toolsPartIn) {
        toolsPartIn.setUpdateUser(getUserId());
        toolsPartIn.setUpdateDate(new Date());
        return toolsPartInDao.update(toolsPartIn);
    }

    @Override
    public int delete(String id) {
        return toolsPartInDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        int rows = 0;
        ToolsPartInEntity toolsPartIn;
        for (String id : ids) {
            toolsPartIn = queryObject(id);
            if (toolsPartIn != null) {
                rows += delete(id);
                // 1、 实时更新仓库库位库存数
                toolsStorePosService.realtimeUpdateStorePosStock(toolsPartIn.getCargoLocation());
                // 2、实时更新备件库存数
                toolsListDao.realtimeUpdateStock(toolsPartIn.getPartCode());
            }
        }
        return rows;
    }

    @Override
    public int statTotalNum(String posId, String month) {
        Integer totalNum = toolsPartInDao.statTotalNum(posId, month);
        return totalNum == null ? 0 : totalNum;
    }

    @Override
    public int review(ToolsPartInEntity toolsPartIn) {
        ToolsStockEntity toolsStockEntity = toolsStockDao.queryByCode(toolsPartIn.getPartCode());
        /**
         * 审核通过 清单 直接更新库存
         */
        if (toolsStockEntity != null) {
            Integer num = 0;
            if (toolsStockEntity.getStockNum() == null) {
                num = toolsPartIn.getInNum();
            } else {
                num = toolsPartIn.getInNum() + toolsStockEntity.getStockNum();
            }
            toolsStockEntity.setStockNum(num);
            toolsStockDao.update(toolsStockEntity);
        }
        return toolsPartInDao.update(toolsPartIn);
    }
}
